package com.bw.a2207a.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.a2207a.base.BaseBean
import com.bw.a2207a.base.BaseViewModel
import com.bw.a2207a.bean.BannerBean
import com.bw.a2207a.bean.CartBean
import com.bw.a2207a.bean.CommentBean
import com.bw.a2207a.bean.GoodsBean
import com.bw.a2207a.db.CartDbUtil
import com.bw.a2207a.bean.SearchHistoryBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.io.File

/**
 *
 * @ClassName: HomeViewModel
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/10/23 12:08 PM
 * @Version: 1.0
 */
class GoodsDetailViewModel : BaseViewModel() {

    val goodsLiveData = MutableLiveData<BaseBean<GoodsBean>>()
    val goodsAddLiveData = MutableLiveData<BaseBean<String>>()
    val goodsCarsLiveData = MutableLiveData<BaseBean<List<CartBean>>>()
    val commentBeanLiveData = MutableLiveData<BaseBean<List<CommentBean>>>()

    //获取商品详情
    fun getGoodsDetail(id: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            goodsLiveData.postValue(apiService.getGoodsDetail(id))
        }
    }

    //商品详情 -添加购物车
    fun addCar(id: Int) {
        val map = HashMap<String, Int>()
        map["count"] = 1//商品个数
        map["goods_id"] = id
        viewModelScope.launch(Dispatchers.IO) {
            goodsAddLiveData.postValue(apiService.addCar(map))
        }
    }

    fun deleteCar(id: Int) {
        val map = HashMap<String, Int>()
        map["ids"] = id
        viewModelScope.launch(Dispatchers.IO) {
            goodsAddLiveData.postValue(apiService.deleteCar(map))
        }
    }

    //获取 购物车
    fun getCar() {
        viewModelScope.launch(Dispatchers.IO) {
            goodsCarsLiveData.postValue(apiService.getCar())
        }
    }

    //获取 评论列表
    fun getComment(id: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            commentBeanLiveData.postValue(apiService.getComment(id))
        }
    }


}